package ncku.account.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import ncku.account.data.SubTransactionList;
import ncku.account.event.Launcher;
import ncku.account.gui.categorySel.categoryShow.categoryPanel;

public class subTransactionTable extends JPanel{
    private tableFactory table;
    private JButton JBSubTransNew;
    private JButton JBSubTransEdit;
    private JButton JBSubTransDelete;
    private JLabel JLsubTrans;
    private transactionModule parentPanel; //儲存母Panel
    
    private SubTransactionList subTransList;
    
  //  public SubTransactionList subTransList = new SubTransactionList();  //新增Transaction時,就先new a subTrasanction List Object
    
    //按下New/Edit會跳出的frame
    class subTransWindows extends JFrame{
    	private JFrame frame;
    	
    	public subTransWindows(String title, int index){
    		frame = new JFrame (title);
        	frame.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
            frame.getContentPane().add (BorderLayout.CENTER,new subTransWindowsPanel(title,index));
            frame.pack();
            frame.setLocation(300, 150);
            frame.setSize(400,200);
            frame.setVisible (true);
    	}
    	
    	//對應subTransWindows的JPanel
        class subTransWindowsPanel extends JPanel{
      		private JLabel JLdescription;
    	    private JLabel JLamount;
    	    private JButton JBSubmit;
    	    private JButton JBCancel;
    	    private JTextField inputDescriptionbox;
    	    private JTextField inputAmountbox;
    	    private String inputDescription;
    	    private int inputAmount;
    	    
    	    private String title;
    	    private int index;
    	    
        	public subTransWindowsPanel(String input, int selectIndex){
        		title = input;
        		index = selectIndex;
        		
    	    	JLdescription = new JLabel ("子細項內容");
    	        JLamount = new JLabel ("金錢");
    	        JBSubmit = new JButton ("確定");
    	        JBCancel = new JButton ("取消");
    	        inputDescriptionbox = new JTextField(10);
    	        inputAmountbox = new JTextField(5);
    	        
    	        if(title.equals("編輯")){  //取得選取欄位的資料
    	        	inputDescriptionbox.setText(subTransList.getSubTrans(index).getSubDescription());
    	        	inputAmountbox.setText(subTransList.getSubTrans(index).getSubStringAmount());
    	        }
    	        
    	        //adjust size and set layout
    	        setPreferredSize (new Dimension (681, 422));
    	        setLayout (null);
    	        
    	        //按下Submit後的Listener(new subTransaction)
    	        JBSubmit.addActionListener(new ActionListener() { 
    			public void actionPerformed(ActionEvent e) 
    			{ 
    				try{
    					inputDescription = inputDescriptionbox.getText();
        		        inputAmount = Integer.parseInt(inputAmountbox.getText());
    				}
    				catch(Exception ex){
    					JOptionPane.showMessageDialog(Launcher.frame, "資料尚未填寫完整！", "提示訊息", JOptionPane.ERROR_MESSAGE);
    					return;
    				}
        		    if(inputAmount<=0){
						JOptionPane.showMessageDialog(Launcher.frame, "金額須為大於零的整數！", "提示訊息", JOptionPane.ERROR_MESSAGE);
						return;
					}
    				
	    		    if(title.equals("新增")){  //新增subTransaction
	    		       	subTransList.addSubTrans(inputDescription, inputAmount);
	    		    }
	    		    else{  //修改subTransaction
	    		      	subTransList.editSubTrans(index, inputDescription, inputAmount);
	    		    }
	    			reset();
	    			frame.dispose();
    			}}); 
    	        
    	        //按下Cancel後的Listener(new subTransaction)
    	        JBCancel.addActionListener(new ActionListener() { 
    			public void actionPerformed(ActionEvent e) 
    			{ 
    				frame.dispose();
    			}}); 

    	        //add components
    	        add (JLdescription);
    	        add (JLamount);
    	        add (JBSubmit);
    	        add (JBCancel);
    	        add (inputDescriptionbox);
    	        add (inputAmountbox);
    	        
    	        //set component bounds (only needed by Absolute Positioning)	       
    	        JLdescription.setBounds (25, 35, 100, 25);
    	        JLamount.setBounds (45, 70, 100, 25);
    	        JBSubmit.setBounds (65, 100, 80, 30);
    	        JBCancel.setBounds (160, 100, 80, 30);
    	        inputDescriptionbox.setBounds (100, 35, 150, 25);
    	        inputAmountbox.setBounds (100, 70, 65, 25);	        
        	}
        }
    }
    
    
    public subTransactionTable(transactionModule input, SubTransactionList inputList){
    	this.parentPanel = input;
    	this.subTransList = inputList;
    	
		JBSubTransNew = new JButton("新增");
		JBSubTransEdit = new JButton("編輯");
		JBSubTransDelete = new JButton("刪除");
		JLsubTrans = new JLabel("交易子細項");
		table = getSubTransTable();
		table.setOpaque(true); //content panes must be opaque
		
		
        //按下New後的Listener(new subTransaction)
        JBSubTransNew.addActionListener(new ActionListener() { 
			public void actionPerformed(ActionEvent e) 
			{ 
				subTransWindows newSub = new subTransWindows("新增",0);
				
			}});
        
       //按下Edit後的Listener(new subTransaction)
        JBSubTransEdit.addActionListener(new ActionListener() { 
			public void actionPerformed(ActionEvent e) 
			{ 
				int index = table.table.getSelectedRow();
				if(index> -1){  //有選擇
					subTransWindows editSub = new subTransWindows("編輯",index);
				}
				else{
					JOptionPane.showMessageDialog(Launcher.frame, "未選擇SubTransaction！", "提示訊息", JOptionPane.ERROR_MESSAGE);
				}
			}});
        
       //按下Delete後的Listener(delete subTransaction)
        JBSubTransDelete.addActionListener(new ActionListener() { 
			public void actionPerformed(ActionEvent e) 
			{ 
				int index = table.table.getSelectedRow();
				if(index > -1){
					subTransList.deleteSubTrans(index);
					reset();
				}
				else{
					JOptionPane.showMessageDialog(Launcher.frame, "未選擇SubTransaction！", "提示訊息", JOptionPane.ERROR_MESSAGE);
				}
			}});
		
		
		
	     //adjust size and set layout
        setPreferredSize (new Dimension (550, 300));
        setLayout (null);
		
        JBSubTransNew.setVisible(true);
        JBSubTransEdit.setVisible(true);
        JBSubTransDelete.setVisible(true);
        JLsubTrans.setVisible(true);
        
        add(JBSubTransNew);
        add(JBSubTransEdit);
        add(JBSubTransDelete);
        add(JLsubTrans);
        add(table);
        
        JLsubTrans.setBounds(20,20,100,25);
        JBSubTransNew.setBounds (150, 20, 80, 30);
        JBSubTransEdit.setBounds (235,20,80,30);
        JBSubTransDelete.setBounds(320, 20, 80, 30);
        table.setBounds(20,60,520,160);
    }
    
    tableFactory getSubTransTable(){
    	tableFactory result;
    	Object [] [] SubTransItems = new  Object [subTransList.getSize()][2];  //顯示在下方Table的SubTransaction object
		String [] columnItems = {"子項目名稱", "金錢"};  //SubTransaction Table的項目
		
		for(int i=0; i<subTransList.getSize(); i++){
			SubTransItems [i] [0] = subTransList.getSubTrans(i).getSubDescription();
			SubTransItems [i] [1] = subTransList.getSubTrans(i).getSubStringAmount();
		}
		
		result = new tableFactory(columnItems,SubTransItems);
		return result;
    }
    
    void reset(){
		table = getSubTransTable();  //更新table
		table.updateUI();  
		parentPanel.resetLayoutTable();  //重畫table
	}
}
